﻿using System;
using System.Configuration;

namespace Fuse8.ContentManagementFramework.SystemFacade
{
    public static class CmsConfiguration
    {
        public const string SiteConfigurationElementMetaFieldName = "cmsConfigurationElement";
        public const string ContentTypeCategoryMetaFieldName = "cmsContentTypeCategories";
        public const string ContentCategoryMetaFieldName = "cmsContentCategory";
        public const string ContextTemplateKey = "ContextTemplateForPreview";

        public const string DefaultImagePath = "default/noimage.png";
        public const string FileUploadPath = "fileUploadPath";
        public const string FileUploadDefaultPath = "~/uploaded_files";       

        private static int _siteId = 0;
        private static string _fileUploadPath;
        private static string _systemSenderEmail;
        private static bool? _isWorkflowEnabled;

        public static int SiteId
        {
            get
            {
                if (_siteId == 0)
                {
                    if (ConfigurationManager.AppSettings["SiteId"] != null)
                    {
                        int.TryParse(ConfigurationManager.AppSettings["SiteId"], out _siteId);
                    }
                }

                return _siteId;
            }
            set
            {
                _siteId = value > 0 ? value : 0;
            }
        }        

        public static string AssetUploadPath
        {
            get
            {
                if (string.IsNullOrEmpty(_fileUploadPath))
                {
                    _fileUploadPath = ConfigurationManager.AppSettings[FileUploadPath] != null ?
                        ConfigurationManager.AppSettings[FileUploadPath] :
                        FileUploadDefaultPath;
                }

                return _fileUploadPath;
            }
            set
            {
                _fileUploadPath = value;
            }
        }

        public static string SystemSenderEmail
        { 
            get
            {
                if (string.IsNullOrEmpty(_systemSenderEmail))
                {
                    _systemSenderEmail = ConfigurationManager.AppSettings["SystemSenderEmail"] != null ?
                        ConfigurationManager.AppSettings["SystemSenderEmail"] :
                        string.Empty;
                }

                return _systemSenderEmail;
            }
            set
            {
                _systemSenderEmail = value;
            }            
        }

        public static bool IsWorkflowEnabled
        {
            get
            {
                if (!_isWorkflowEnabled.HasValue)
                {
                    string configValue = ConfigurationManager.AppSettings["EnabledWorkflow"] ?? string.Empty;

                    bool result = false;

                    bool.TryParse(configValue, out result);

                    _isWorkflowEnabled = result;
                }

                return _isWorkflowEnabled.Value;
            }
            set
            {
                _isWorkflowEnabled = value;
            }
        }

        /// <summary>
        /// Gets path to editor custom tools configuration xml
        /// </summary>
        public static string CustomEditorToolPath
        {
            get { return ConfigurationManager.AppSettings["CMS:CustomEditorToolPath"]; }
        }

        private static long? imageQuality;

        public static long ResizerImageQuality
        {
            get
            {

                if (!imageQuality.HasValue)
                {
                    long setting;
                    if (!long.TryParse(ConfigurationManager.AppSettings["ResizerImageQuality"], out setting))
                    {
                        imageQuality = 90L;
                    }
                    else
                    {
                        imageQuality = setting;
                    }

                    if (imageQuality.Value == 0)
                    {
                        imageQuality = 90L;
                    }
                }
                return imageQuality.Value;
            }
        }
    }
}
